/*
	@baukh20150121:
	1、增加注释，可能不对，如有错误请纠正。
	2、修改删除机制为可随意删除非唯一的box
*/
define([], function() {
    // 通用复制组件
    function CopyBox(ctner, settings){
        this.container=ctner
        this.els=[]
        $.extend(this, settings)
    }
    CopyBox.prototype={
        box:"<div class='well cbox'><a class='close closeBtn' cb-del href='javascript:void(0);'>×</a>",
		/*
			@增加box
		*/
        add:function(){
            var self=this
            self.beforeAdd(function(){
                var $box = $(self.box);
                $(self.container).append($box);
				self.bindRemove($box);
                self.perInit($box);
                self.els.push($box);
				$box.parent().find('[cb-del]').show();
				self.afterAdd($box);
                $box.trigger('addEvent');//触发添加事件
            })
        },
		/*
			@初始化box
		*/
        init:function(){
            var self=this;
            self.beforeAdd(function(data){
                var $box = $(self.box);
                $(self.container).append($box);
				self.bindRemove($box);
                self.perInit($box);
                self.els.push($box);
				$("[cb-del]", $box).hide();
				self.afterAdd($box);
            })
        },
		/*初始化box*/
        perInit:$.noop,
		/*增加前*/
        beforeAdd:function(cb){
            cb();
        },
		/*增加后*/
		afterAdd:$.noop,
		/*绑定删除操作*/
		bindRemove:function($box){
			var _this = this;
			var cbDel = $("[cb-del]", $box),
				allDel;
			cbDel.unbind('click');
			cbDel.click(function(){
				var i = _this.els.indexOf($box);
				_this.beforeRemove($box);
				$box.hide();
				allDel = $box.parent().find('[cb-del]');
				allDel.length == 2 ? allDel.hide() : '';
				$box.remove();
				_this.afterRemove();
				_this.els.splice(i, 1)
			})
		},
		/*删除前*/
        beforeRemove:$.noop,
		/*删除后*/
        afterRemove:$.noop,
		/*循环获取每一个box的值*/
        perFetch:$.noop,
        fetchVal:function(){
            var self=this, result=[]
            $.each(this.els,function(i, el){
                result.push(self.perFetch(el))
            })
            return result
        }
    }

	//单选复制组件
    function RadioCopyBox(ctner, settings){
        this.container=ctner
        this.els=[]
        this.selected=[]
        $.extend(this, settings)
    }
    $.extend(RadioCopyBox.prototype, CopyBox.prototype, {
        beforeAdd:function(cb){
            if (this._selected().length!=this.data.length){
                cb()
            }
        },
        perInit:function($box){
            var self=this, data
            var html = self.perHtml()
            $box.append(html)
            var selected = self._selected()
            self.data.some(function(ele){
                if(selected.indexOf(ele.val)<0){
                    data=ele.val
                    return true
                }
            })
            var rd = $("input[value='"+data+"']", $box)
            if(rd.length){
                rd[0].checked=true
            }
            var old = $(':radio:checked', $box).val();
            $box.on("click", ":radio", function(){
                var i =self.els.indexOf($box)
                var curVal = this.value;
                var sameVal = _.filter(self._selected(), function(v){ return v === curVal});
                if(sameVal.length>1){
                    $("input[value='"+old+"']", $box)[0].checked=true
                     self.msg()
                }
                old = $(':radio:checked', $box).val();
            })
        },
        _selected:function(){
            return _.map(this.els, function(el){
                return $(":checked", el).val();
            })
        },
        perFetch:function($box){
            return $(":checked", $box).val();
        }
    })
	//多选复制组件
    function CheckCopyBox(ctner, settings){
        this.container=ctner
        this.els=[]
        $.extend(this, settings)
    }
    $.extend(CheckCopyBox.prototype, CopyBox.prototype, {
        perInit:function($box){
            var html = this.perHtml()
            $box.append(html)
        },
        perFetch:function($box){
            var checkedArr=[]
            $(":checked", $box).each(function(i, el){
                checkedArr.push(el.value)
            })
            return checkedArr;
        }
    })
    return {
        "CopyBox": CopyBox,
        "RadioCopyBox": RadioCopyBox,
        "CheckCopyBox": CheckCopyBox
    }
});
